/*
 * Created by JFormDesigner on Wed Dec 07 00:06:07 ICT 2011
 */

package realestatemarketclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.jgoodies.forms.factories.*;
import com.jgoodies.forms.layout.*;
import java.util.Date;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import java.util.List;

/**
 * @author Tran Cong
 */
public class NewsInMonthForm extends JFrame {
    public NewsInMonthForm() {
        initComponents();
    }
    
    private void loadData(int month, int year){
        try{
            countReal = RealEstateService.getRequest().getAllCountReal(month, year).getCOUNTREALESTATE();
            DefaultTableModel dtm = new DefaultTableModel();
            dtm.addColumn("STT");
            dtm.addColumn("Name");
            dtm.addColumn("Total");
            dtm.addColumn("PriceUnit");
            int i = 1;
            for (REMService.COUNTREALESTATE cr : countReal){
                Vector v = new Vector();
                v.add(i++);
                v.add(cr.getName());
                v.add(cr.getTotal());
                v.add(cr.getUnitName());
                dtm.addRow(v);
                }

                newsTable.setModel(dtm);
            }
        catch(Exception e){
            JOptionPane.showMessageDialog(this, e.toString());
        }
    }
    
    private void okButtonActionPerformed(ActionEvent e) {
        if(yearTextField.getText().length()!=4)
            JOptionPane.showMessageDialog(this, "Year is not true. Please try again.");
        else
        {
            loadData(monthComboBox.getSelectedIndex()+1,Integer.valueOf(yearTextField.getText()));
        }
    }

    private void thisWindowOpened(WindowEvent e) {
        // TODO add your code here
        Date today = new Date(System.currentTimeMillis());
        monthComboBox.setSelectedIndex(today.getMonth());
        yearTextField.setText(String.valueOf(1900 + today.getYear()));
        loadData(today.getMonth() + 1, 1900 + today.getYear());
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        // Generated using JFormDesigner Evaluation license - Tran Cong
        label1 = new JLabel();
        monthComboBox = new JComboBox();
        label2 = new JLabel();
        yearTextField = new JTextField();
        okButton = new JButton();
        scrollPane1 = new JScrollPane();
        newsTable = new JTable();

        //======== this ========
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                thisWindowOpened(e);
            }
        });
        Container contentPane = getContentPane();
        contentPane.setLayout(new FormLayout(
            "63dlu, 59dlu, 55dlu, 68dlu, 51dlu, 55dlu",
            "default, 21dlu, 7dlu, default, 18dlu, 129dlu, 16dlu"));

        //---- label1 ----
        label1.setText("Month");
        contentPane.add(label1, CC.xy(2, 2));

        //---- monthComboBox ----
        monthComboBox.setModel(new DefaultComboBoxModel(new String[] {
            "1",
            "2",
            "3",
            "4",
            "5",
            "6",
            "7",
            "8",
            "9",
            "10",
            "11",
            "12"
        }));
        monthComboBox.setSelectedIndex(11);
        contentPane.add(monthComboBox, CC.xy(3, 2));

        //---- label2 ----
        label2.setText("Year");
        contentPane.add(label2, CC.xy(2, 4));
        contentPane.add(yearTextField, CC.xy(3, 4));

        //---- okButton ----
        okButton.setText("OK");
        okButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                okButtonActionPerformed(e);
            }
        });
        contentPane.add(okButton, CC.xy(5, 4));

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(newsTable);
        }
        contentPane.add(scrollPane1, CC.xywh(2, 6, 4, 1));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    // Generated using JFormDesigner Evaluation license - Tran Cong
    private JLabel label1;
    private JComboBox monthComboBox;
    private JLabel label2;
    private JTextField yearTextField;
    private JButton okButton;
    private JScrollPane scrollPane1;
    private JTable newsTable;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
    private List<REMService.COUNTREALESTATE> countReal;
}
